import 'package:flutter/widgets.dart';

/// XHKeepAliveWrapper can keep the item(s) of scrollview alive, **Not dispose**.
class XHKeepAliveWrapper extends StatefulWidget {
  const XHKeepAliveWrapper({
    super.key,
    this.keepAlive = true,
    required this.child,
  });
  final bool keepAlive;
  final Widget child;

  @override
  _XHKeepAliveWrapperState createState() => _XHKeepAliveWrapperState();
}

class _XHKeepAliveWrapperState extends State<XHKeepAliveWrapper> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  void didUpdateWidget(covariant XHKeepAliveWrapper oldWidget) {
    if (oldWidget.keepAlive != widget.keepAlive) {
      updateKeepAlive();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    //print("XHKeepAliveWrapper dispose");
    super.dispose();
  }

  @override
  bool get wantKeepAlive => widget.keepAlive;
}
